﻿namespace EggLink.DanhengServer.Configuration;

/// <summary>
/// 主配置容器，包含所有服务器配置模块
/// </summary>
public class ConfigContainer
{
    /// <summary>HTTP服务器配置</summary>
    public HttpServerConfig HttpServer { get; set; } = new();

    /// <summary>SSL证书和密钥存储配置</summary>
    public KeyStoreConfig KeyStore { get; set; } = new();

    /// <summary>游戏服务器网络配置</summary>
    public GameServerConfig GameServer { get; set; } = new();

    /// <summary>文件路径配置</summary>
    public PathConfig Path { get; set; } = new();

    /// <summary>数据库连接配置</summary>
    public DatabaseConfig Database { get; set; } = new();

    /// <summary>服务器游戏性选项</summary>
    public ServerOption ServerOption { get; set; } = new();

    /// <summary>资源下载URL配置</summary>
    public DownloadUrlConfig DownloadUrl { get; set; } = new();

    /// <summary>MUIP服务器配置</summary>
    public MuipServerConfig MuipServer { get; set; } = new();
}

/// <summary>
/// HTTP服务器配置
/// </summary>
public class HttpServerConfig
{
    /// <summary>服务器监听地址（默认：0.0.0.0）</summary>
    public string BindAddress { get; set; } = "0.0.0.0";

    /// <summary>对外公开的访问地址（默认：127.0.0.1）</summary>
    public string PublicAddress { get; set; } = "127.0.0.1";

    /// <summary>HTTP服务端口号（默认：443）</summary>
    public int Port { get; set; } = 443;

    /// <summary>是否启用SSL（默认：false）</summary>
    public bool UseSSL { get; set; } = false;

    /// <summary>
    /// 获取对外显示的访问地址（包含协议头）
    /// </summary>
    public string GetDisplayAddress ()
    {
        return (UseSSL ? "https" : "http") + "://" + PublicAddress + ":" + Port;
    }

    /// <summary>
    /// 获取绑定地址的显示格式（包含协议头）
    /// </summary>
    public string GetBindDisplayAddress ()
    {
        return (UseSSL ? "https" : "http") + "://" + BindAddress + ":" + Port;
    }
}

/// <summary>
/// SSL证书配置
/// </summary>
public class KeyStoreConfig
{
    /// <summary>证书文件路径（默认：certificate.p12）</summary>
    public string KeyStorePath { get; set; } = "certificate.p12";

    /// <summary>证书密码（默认：123456）</summary>
    public string KeyStorePassword { get; set; } = "123456";
}

/// <summary>
/// 游戏服务器配置
/// </summary>
public class GameServerConfig
{
    /// <summary>服务器监听地址（默认：0.0.0.0）</summary>
    public string BindAddress { get; set; } = "0.0.0.0";

    /// <summary>对外公开的访问地址（默认：127.0.0.1）</summary>
    public string PublicAddress { get; set; } = "127.0.0.1";

    /// <summary>游戏服务器端口号（默认：23301）</summary>
    public uint Port { get; set; } = 23301;
    
    /// <summary>服务器ID标识（默认：dan_heng）</summary>
    public string GameServerId { get; set; } = "dan_heng";

    /// <summary>服务器显示名称</summary>
    public string GameServerName { get; set; } = "I Love You";

    /// <summary>服务器描述（默认：星穹铁道服务器的重新实现）</summary>
    public string GameServerDescription { get; set; } = "A re-implementation of StarRail server";

    /// <summary>是否启用数据包加密（默认：true）</summary>
    public bool UsePacketEncryption { get; set; } = true;

    /// <summary>
    /// 获取对外显示的服务器地址（IP:Port格式）
    /// </summary>
    public string GetDisplayAddress ()
    {
        return PublicAddress + ":" + Port;
    }
}

/// <summary>
/// 文件路径配置
/// </summary>
public class PathConfig
{
    /// <summary>资源文件路径（默认：Resources）</summary>
    public string ResourcePath { get; set; } = "Resources";

    /// <summary>配置文件路径（默认：Config）</summary>
    public string ConfigPath { get; set; } = "Config";

    /// <summary>数据库文件路径（默认：Config/Database）</summary>
    public string DatabasePath { get; set; } = "Config/Database";

    /// <summary>日志文件路径（默认：Logs）</summary>
    public string LogPath { get; set; } = "Logs";

    /// <summary>插件路径（默认：Plugins）</summary>
    public string PluginPath { get; set; } = "Plugins";
}

/// <summary>
/// 数据库配置
/// </summary>
public class DatabaseConfig
{
    /// <summary>数据库类型（sqlite/mysql，默认：sqlite）</summary>
    public string DatabaseType { get; set; } = "sqlite";

    /// <summary>SQLite数据库文件名（默认：danheng.db）</summary>
    public string DatabaseName { get; set; } = "danheng.db";

    /// <summary>MySQL主机地址（默认：127.0.0.1）</summary>
    public string MySqlHost { get; set; } = "127.0.0.1";

    /// <summary>MySQL端口号（默认：3306）</summary>
    public int MySqlPort { get; set; } = 3306;

    /// <summary>MySQL用户名（默认：root）</summary>
    public string MySqlUser { get; set; } = "root";

    /// <summary>MySQL密码（默认：123456）</summary>
    public string MySqlPassword { get; set; } = "123456";

    /// <summary>MySQL数据库名（默认：danheng）</summary>
    public string MySqlDatabase { get; set; } = "danheng";
}

/// <summary>
/// 服务器游戏性选项
/// </summary>
public class ServerOption
{
    /// <summary>初始开拓者等级（默认：1）</summary>
    public int StartTrailblazerLevel { get; set; } = 1;

    /// <summary>是否自动提升世界等级（默认：true）</summary>
    public bool AutoUpgradeWorldLevel { get; set; } = true;

    /// <summary>是否启用任务系统（实验性，默认：true）</summary>
    public bool EnableMission { get; set; } = true;

    /// <summary>是否自动点亮区域（默认：true）</summary>
    public bool AutoLightSection { get; set; } = true;

    /// <summary>服务器默认语言（默认：EN）</summary>
    public string Language { get; set; } = "EN";

    /// <summary>备用语言（默认：EN）</summary>
    public string FallbackLanguage { get; set; } = "EN";

    /// <summary>默认权限集合（默认：["*"]）</summary>
    public HashSet<string> DefaultPermissions { get; set; } = ["*"];

    /// <summary>服务器公告配置</summary>
    public ServerAnnounce ServerAnnounce { get; set; } = new();

    /// <summary>服务器资料配置</summary>
    public ServerProfile ServerProfile { get; set; } = new();

    /// <summary>是否自动创建用户（默认：true）</summary>
    public bool AutoCreateUser { get; set; } = true;

    /// <summary>是否保存个人调试文件（默认：false）</summary>
    public bool SavePersonalDebugFile { get; set; } = false;
}

/// <summary>
/// 服务器公告配置
/// </summary>
public class ServerAnnounce
{
    /// <summary>是否启用公告（默认：true）</summary>
    public bool EnableAnnounce { get; set; } = true;

    /// <summary>公告</summary>
    public string AnnounceContent { get; set; } = "崩坏星穹铁道";
}

/// <summary>
/// 服务器资料配置
/// </summary>
public class ServerProfile
{
    /// <summary>服务器名称（默认：Server）</summary>
    public string Name { get; set; } = "Server";

    /// <summary>服务器UID（默认：80）</summary>
    public int Uid { get; set; } = 80;

    /// <summary>服务器签名（默认：输入/help查看命令列表）</summary>
    public string Signature { get; set; } = "Type /help for a list of commands";

    /// <summary>服务器等级（默认：1）</summary>
    public int Level { get; set; } = 1;

    /// <summary>头像ID（默认：200105）</summary>
    public int HeadIcon { get; set; } = 200105;

    /// <summary>聊天气泡ID（默认：220001）</summary>
    public int ChatBubbleId { get; set; } = 220001;

    /// <summary>展示角色ID（默认：1001）</summary>
    public int DisplayAvatarId { get; set; } = 1001;

    /// <summary>展示角色等级（默认：1）</summary>
    public int DisplayAvatarLevel { get; set; } = 1;
}

/// <summary>
/// 资源下载URL配置
/// </summary>
public class DownloadUrlConfig
{
    /// <summary>AssetBundle资源下载URL（默认：null）</summary>
    public string? AssetBundleUrl { get; set; } = null;

    /// <summary>额外资源下载URL（默认：null）</summary>
    public string? ExResourceUrl { get; set; } = null;

    /// <summary>Lua脚本下载URL（默认：null）</summary>
    public string? LuaUrl { get; set; } = null;

    /// <summary>热修复文件下载URL（默认：null）</summary>
    public string? IfixUrl { get; set; } = null;
}

/// <summary>
/// MUIP服务器配置
/// </summary>
public class MuipServerConfig
{
    /// <summary>管理员密钥（默认："None"）</summary>
    public string AdminKey{ get; set; } = "None";
}